package com.ynny.librarysystemmanager.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.ynny.librarysystemmanager.common.R;
import com.ynny.librarysystemmanager.entity.Book;
import com.ynny.librarysystemmanager.entity.BookDTO;
import com.ynny.librarysystemmanager.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @GetMapping("/list")
    public R list( Book book){

        List<Book> books = bookService.findAllBooks(book);

        return R.ok(books);
    }

    @GetMapping("/page")
    public R page(@RequestBody BookDTO dto ){

//     PageInfo
        PageInfo pageInfo = bookService.page(dto);

        return R.ok(pageInfo);
    }



    @PostMapping("/edit")
    public R edit(@RequestBody Book book){

        if (ObjectUtil.isEmpty(book.getBookId())){
            int i = bookService.addBook(book);
        }else {
            int i = bookService.editBook(book);
        }
        return R.ok(book);
    }

    @DeleteMapping("/disable/{bookId}/{status}")
    public R disable(@PathVariable("bookId") Integer bookId,
                     @PathVariable("status") Integer status){

        int i = bookService.disableBook(bookId, status);

        return R.ok(i);

    }


}
